import axios from 'axios'
import { getToken } from './auth'
// import { getToken } from '@/utils/auth'
const service = axios.create({
  baseURL: 'https://api-hmzs.itheima.net/tj',
  timeout: 5000 // request timeout
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    //本来这里从vuex里面取Token,但是这里没有vuex,以后也会从pinia里面取
    // const token = getToken()
    // if (token) {
    //   config.headers.Authorization = token
    // }
    //版本1.0 这里我的token写死,因为从后台拿到的token手动存到了cookie里面,但是没有了
    // config.headers.Authorization ="eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImUxNjBkMzBiLTc0NGItNGVjZS1hZGViLWZjNjU1MWU4MjljOSJ9.SLG5o8CsfBZTvNp6RUA-3t1b5nkb7suzkU3uU3Am8jHrlzqYnTdGBMAjbUK_hvacczxJTaOBc1XzooSjG45yzQ"

    //版本2.0 修改了后台的token存储方式,现在可以从cookie中获取token.
    const token=getToken()
    if(token){
      config.headers.Authorization=token
    }

    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
